package m2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;


import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/*
    2022/1/20
    11:36
*/public class Producer {

    public static void main(String[] args) throws Exception, TimeoutException {
        //连接服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection(); //获取链接
        Channel channel = con.createChannel(); //建立通信通道

        //创建队列
        /**
         * queue:队列名称
         * durable:队列是否持久化
         * exclusive:是否是排它队列，单个消费者独占队列
         * autoDelete: 当最后一个消费者断开连接后是否删除队列
         * arguments: 队列的其他属性
         */
        channel.queueDeclare("task_queue!",true, false, false, null);

        //向队列发送数据
        while (true) {
            System.out.println("请输入消息:");
            String s = new Scanner(System.in).nextLine();
            channel.basicPublish("","task_queue!", MessageProperties.PERSISTENT_BASIC,s.getBytes());
            System.out.println("发送成功~");
        }
    }
}
